package com.example.statistics.ui.car

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.example.statistics.R
import com.example.statistics.bean.CarInfoBean
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.messageDialog
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import com.example.statistics.ex.onActionLongClicked
import com.example.statistics.ex.toUpperCase
import kotlinx.android.synthetic.main.layout_search_bar.*


class CarListActivity : BaseLoadingListActivity<CarInfoBean>() {
    var action: String? = null

    override val title: String
        get() = "车辆信息"
    override val url: String
        get() = UrlConfig.CAR_LIST
    override val itemLayoutRes: Int
        get() = R.layout.list_item_car
    override val inputSearch: Boolean
        get() = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        action = intent.getStringExtra(Constants.ACTION)
        et_search_input.toUpperCase()
        topBarRightImage {
            activityForResult<CarEditActivity>(Constants.ACTION to Constants.ACTION_ADD) {
                refresh()
            }
        }
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: CarInfoBean, position: Int) {
        injector.text(R.id.tv_title, bean.salesArea)
            .text(R.id.tv_area, "${bean.plateNumber} | ${bean.carLength}")
            .text(R.id.tv_time, bean.logisticsCompany)
            .text(R.id.tv_phone, bean.driverPhone)
            .clicked(R.id.tv_phone) {
                val intent = Intent(Intent.ACTION_DIAL)
                intent.data = Uri.parse("tel:" + bean.driverPhone)
                startActivity(intent)
            }
            .onActionLongClicked(
                "复制" to {
                    activityForResult<CarEditActivity>(
                        Constants.ACTION to Constants.ACTION_ADD,
                        Constants.DATA to bean
                    ) {
                        refresh()
                    }
                },
                "编辑" to {
                    activityForResult<CarEditActivity>(
                        Constants.ACTION to Constants.ACTION_EDIT,
                        Constants.DATA to bean
                    ) {
                        refresh()
                    }
                },
                "删除" to {
                    messageDialog(msg = "是否删除${bean.plateNumber}?") { _, _ ->
                        httpGet<CarInfoBean>(UrlConfig.CAR_DELETE, config = {
                            listParams = listOf("id" to bean.id)
                            isSuccessToast = true
                        }) {
                            refresh()
                        }
                    }
                }
            )
            .clicked {
                if (Constants.SELECTOR == action) {
                    activityFinish(Constants.DATA to bean)
                } else {
                    activityForResult<CarEditActivity>(
                        Constants.ACTION to Constants.ACTION_EDIT,
                        Constants.DATA to bean
                    ) {
                        refresh()
                    }
                }
            }
    }


}